package com.appheader.webfragmentviewpagertest;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;

public class PopupWindowActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btn = null;
    private Button btn2 = null;
    private boolean click = false;
    PopupWindow mPopupWindow = null;
    View vPopunwind = null;
    Drawable mDrawable1 = null;
    Drawable mDrawable2 = null;
    Resources r = null;
    LayoutInflater mLayoutInflater = null;
    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient client;
    private CheckBox checkBox;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_popup_window);
        btn = (Button) findViewById(R.id.btn);
        btn.setOnClickListener(this);
        r = this.getResources();

//        mDrawable1 = r.getDrawable(R.drawable.head1);
//        mDrawable2 = r.getDrawable(R.drawable.head2);

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }

    @Override
    public void onClick(View v) {
        Context mContext = PopupWindowActivity.this;
        if (v.getId() == R.id.btn) {
            mLayoutInflater = (LayoutInflater) mContext
                    .getSystemService(LAYOUT_INFLATER_SERVICE);
            vPopunwind = mLayoutInflater.inflate(
                    R.layout.pop_test, null, false);

//          mPopupWindow = new PopupWindow(vPopunwind,LayoutParams.WRAP_CONTENT,
//                  LayoutParams.WRAP_CONTENT);
            mPopupWindow = new PopupWindow(mContext);
            mPopupWindow.setContentView(vPopunwind);
            mPopupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
            mPopupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
            mPopupWindow.setFocusable(true);
            mPopupWindow.setOutsideTouchable(true);
            mPopupWindow.setBackgroundDrawable(new ColorDrawable(0));

            mPopupWindow.showAtLocation(mLayoutInflater.inflate(
                    R.layout.activity_popup_window, null, false), Gravity.BOTTOM | Gravity.CENTER, 0, 0);

//            btn2 = (Button) vPopunwind.findViewById(R.id.btn2);
            checkBox = (CheckBox) vPopunwind.findViewById(R.id.switch_off_on);
//            btn2.setOnClickListener(this);
        }

//        if (v.getId() == R.id.btn2) {
            if (checkBox.isChecked()) {
                FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) vPopunwind.getLayoutParams();
                lp.height = 400;
                lp.width = 200;
                vPopunwind.setLayoutParams(lp);
                vPopunwind.setBackgroundDrawable(new BitmapDrawable());
                mPopupWindow.setContentView(vPopunwind);
                mPopupWindow.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
                mPopupWindow.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
                mPopupWindow.setFocusable(true);
                mPopupWindow.setOutsideTouchable(true);

                mPopupWindow.showAtLocation(mLayoutInflater.inflate(
                        R.layout.activity_popup_window, null, false), Gravity.BOTTOM | Gravity.CENTER, 0, 0);
            } else {
                FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) vPopunwind.getLayoutParams();
                lp.height = 200;
                lp.width = 200;
                vPopunwind.setLayoutParams(lp);
                vPopunwind.setBackgroundDrawable(new BitmapDrawable());
                mPopupWindow.setContentView(vPopunwind);
                mPopupWindow.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
                mPopupWindow.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
                mPopupWindow.setFocusable(true);
                mPopupWindow.setOutsideTouchable(true);


                mPopupWindow.showAtLocation(mLayoutInflater.inflate(
                        R.layout.activity_popup_window, null, false), Gravity.BOTTOM | Gravity.CENTER, 0, 0);
            }

//            click = !click;
//        }

    }


}
